package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.MyEditingPictureAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.EventRefreshAttentionCicle;
import com.hangzhou.welbeing.welbeinginstrument.bean.LoadPicturesDateBeans;
import com.hangzhou.welbeing.welbeinginstrument.bean.PostedEditing00Bean;
import com.hangzhou.welbeing.welbeinginstrument.bean.VideoPostDateBeans;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.utils.GlideImageLoader;
import com.hangzhou.welbeing.welbeinginstrument.utils.UriUtils;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/2/2
 */

public class ReleaseEditingActivity extends Activity {
    @Bind(R.id.activity_release_editing_iv_return)
    ImageView activityReleaseEditingIvReturn;
    @Bind(R.id.activity_release_editing_iv_delect_video)
    ImageView activityReleaseEditingIvDelectVideo;
    @Bind(R.id.activity_release_editing_rl_delect_video)
    RelativeLayout activityReleaseEditingRlDelectVideo;
    @Bind(R.id.activity_release_editing_rl)
    RelativeLayout activityReleaseEditingRl;

    @Bind(R.id.activity_release_editing_tv_posted_01)
    TextView activityReleaseEditingTvPosted;
    @Bind(R.id.activity_release_editing_tv_content)
    TextView activityReleaseEditingTvContent;
    @Bind(R.id.activity_release_editing_et_content)
    EditText activityReleaseEditingEtContent;
    @Bind(R.id.activity_release_editing_rl_content)
    RelativeLayout activityReleaseEditingRlContent;
    @Bind(R.id.activity_release_editing_iv_jZVideoPlayerStandard)
    JZVideoPlayerStandard activityReleaseEditingIvJZVideoPlayerStandard;
    @Bind(R.id.activity_release_editing_recyclerView)
    RecyclerView activityReleaseEditingRecyclerView;
    @Bind(R.id.activity_release_editing_iv_pictrue)
    ImageView activityReleaseEditingIvPictrue;
    @Bind(R.id.activity_release_editing_iv_vivod)
    ImageView activityReleaseEditingIvVivod;
    @Bind(R.id.activity_release_editing_progressbar)
    ProgressBar activityReleaseEditingProgressbar;

    private Bitmap head;//头像Bitmap
    private int anInt = 0;
    private ImagePicker imagePicker;
    ArrayList<ImageItem> images = null;
    private HashSet<File> mlist = new HashSet();
    private List<File> mylist = new ArrayList();
    private String circle_id;
    private String circle_name;
    private String user_id;
    private Uri url;
    private boolean flag = true;
    private boolean flag_01 = true;
    private boolean flag_02 = true;
    private TextView tv_content;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_editing);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(true, this);

        Intent intent = getIntent();
        circle_id = intent.getStringExtra("circle_id");
        circle_name = intent.getStringExtra("circle_name");
        if(circle_id == null || circle_name == null ){
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }else if(TextUtils.isEmpty(circle_id) || TextUtils.isEmpty(circle_name)){
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
        // todo 获取的 ImagePicker 对象
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader()); //TODO 设置图片加载器
        imagePicker.setMultiMode(true);  //TODO 多选
        imagePicker.setShowCamera(true);  //TODO 显示相机 显示拍照按钮
        imagePicker.setSaveRectangle(true); //TODO 是否按矩形区域保存图片
        // TODO 设置图片保存宽高 保存文件的宽高
        imagePicker.setOutPutX(800);
        imagePicker.setOutPutY(800);

        GridLayoutManager layoutManager =   new GridLayoutManager(this,3);
        layoutManager.setOrientation(GridLayoutManager.VERTICAL);
        activityReleaseEditingRecyclerView.setLayoutManager(layoutManager);
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        user_id = sp.getString("user_id", "");
        activityReleaseEditingEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                activityReleaseEditingTvContent.setText("");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.activity_release_editing_iv_return, R.id.activity_release_editing_tv_posted_01,
            R.id.activity_release_editing_iv_pictrue, R.id.activity_release_editing_iv_vivod,
            R.id.activity_release_editing_rl_delect_video,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_release_editing_iv_return:
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
            case R.id.activity_release_editing_tv_posted_01: // TODO 发布
                releasePosts();
                break;
            case R.id.activity_release_editing_iv_pictrue: // TODO 选择图片
                choosePicture();
                break;
            case R.id.activity_release_editing_iv_vivod:  //TODO 选择视频
                chooseVideo();
                break;
            case R.id.activity_release_editing_rl_delect_video: //TODO 删除视频
                delectVideo();
                break;
        }
    }
    /**
     * @desc 删除视频
     * */
    private void delectVideo() {
        activityReleaseEditingIvPictrue.setVisibility(View.VISIBLE);
        activityReleaseEditingIvVivod.setVisibility(View.VISIBLE);
        activityReleaseEditingRl.setVisibility(View.GONE);
    }

    /**
     * @desc 选择视频
     * */
    private void chooseVideo() {
        activityReleaseEditingIvPictrue.setVisibility(View.GONE);
        activityReleaseEditingRl.setVisibility(View.VISIBLE);
        activityReleaseEditingRecyclerView.setVisibility(View.GONE);
        selectvideo();
        anInt = 2;
    }

    /**
     * @desc 选择图片
     * */
    private void choosePicture() {
        activityReleaseEditingIvVivod.setVisibility(View.GONE);
        activityReleaseEditingRl.setVisibility(View.GONE);
        activityReleaseEditingRecyclerView.setVisibility(View.VISIBLE);
        //TODO 开启相册
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
        startActivityForResult(intent, 1);
        anInt = 1;
        mlist.clear();
    }

    /**
     * @desc 发布帖子
     * */
    private void releasePosts() {
        String arrlent = activityReleaseEditingEtContent.getText().toString().toString();
        if(!TextUtils.isEmpty(arrlent)){
            if(arrlent.length() >= 5){
                if(anInt == 1){  // TODO 发布图片
                    if(flag_01){
                        flag_01 = false;
                        showProgress("正在上传图片");
                        publishPictures(arrlent);
                    }
                }else if(anInt ==2){ // TODO 发布视频
                    if(url != null){
                        File file = new File(UriUtils.getPath(this,url));
                        if( file.exists()){
                            if(flag){
                                flag = false;
                                showProgress("正在上传视频");
                                publishVideo(arrlent,file);
                            }
                        }else{
                            Toast.makeText(ReleaseEditingActivity.this,"视频文件不存在",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(ReleaseEditingActivity.this,"未获取到视频的url",Toast.LENGTH_SHORT).show();
                    }
                }else if(anInt == 0){  // TODO 单纯的上传 帖子内容
                    if(flag_02){
                        flag_02 = false;
                        showProgress("正在上传帖子");
                        submitPictures_01(arrlent,user_id,circle_id,circle_name,"");
                    }


                }
            }else{
                Toast.makeText(ReleaseEditingActivity.this,"帖子内容不能小于5个字",Toast.LENGTH_SHORT).show();
            }

        }else{
            Toast.makeText(ReleaseEditingActivity.this,"请先输入帖子内容",Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * @desc 显示进度
     * */
    private void showProgress(String str) {
        popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_show_progress_popupwindow_style,null);
        TextView tv_content = (TextView) view.findViewById(R.id.layout_show_progress_popupwindow_style_tv_content);
        tv_content.setText(str);
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        View rootview = View.inflate(this,R.layout.activit_release_similar_profile_page_focused,null);
        popupWindow.showAtLocation(rootview,  Gravity.CENTER,0,0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == ImagePicker.RESULT_CODE_ITEMS){
            if (data != null && requestCode == 1) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                final MyEditingPictureAdapter adapter = new MyEditingPictureAdapter(images,this,imagePicker);
                //Todo  RecyclerView 的初始化没做
                activityReleaseEditingRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(View view) {  //todo 图片放大
                    }
                    @Override
                    public void onItemFirstButtonClick(View v, int position) {
                        images.remove(position);
                        adapter.delectItem(position);
                    }
                });
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
        if(requestCode == 2){
            if(data != null){
                url = data.getData();
                    if(url != null){
                        activityReleaseEditingIvJZVideoPlayerStandard.setUp(String.valueOf(url), JZVideoPlayer.SCREEN_WINDOW_LIST,"");
                        Glide.with(this).load(String.valueOf(url)).into(activityReleaseEditingIvJZVideoPlayerStandard.thumbImageView);
                    }else{
                        Toast.makeText(this,"视频不存在",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(this,"选择本地视频s数据为空",Toast.LENGTH_LONG).show();
                }
        }
    }
    /**
     * @desc 发布图片
     * @param arrlent*/
    private void publishPictures(String arrlent) {
        if(images != null ){
            // TODO 图片转文件
            for (int i = 0; i < images.size(); i++) {
                File file = new File(images.get(i).path);
                mlist.add(file);
            }
            //TODO 判断文件的个数 提交到后台
            determineNumberFiles(mlist,arrlent);
        }else{
            Toast.makeText(ReleaseEditingActivity.this,"请先选择图片",Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * @desc 判断文件的个数 提交到后台
     * */
    private void determineNumberFiles(HashSet<File> mlist, String arrlent) {
        mylist.clear();
        if(mlist.size() == 0){
            submitPictures_01(arrlent,user_id,circle_id,circle_name,"");
        }else if(mlist.size() == 1){
            for (File file : mlist) {
                mylist.add(file);
            }
            // todo 图片提交后台转换成路径
            changePath_01(mylist,arrlent);
        }else if(mlist.size() == 2){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_02(mylist,arrlent);
        }else if(mlist.size() == 3){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_03(mylist,arrlent);
        }else if(mlist.size() == 4){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_04(mylist,arrlent);
        }else if(mlist.size() == 5){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_05(mylist,arrlent);
        }else if(mlist.size() == 6){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_06(mylist,arrlent);
        }else if(mlist.size() == 7){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_07(mylist,arrlent);
        }else if(mlist.size() == 8){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_08(mylist,arrlent);
        }else if(mlist.size() == 9){
            for (File file : mlist) {
                mylist.add(file);
            }
            changePath_09(mylist,arrlent);
        }else{
            Toast.makeText(this,"图片超出九张",Toast.LENGTH_LONG).show();
        }

    }
    /**
     * @desc 提交帖子 0图片
     * @param arrlent   圈子内容
     * @param user_id  发帖人id
     * @param circle_id  圈子id
     * @param circle_name  圈子名字
     * @param po_url    图片路径
     * */
    private void submitPictures_01(String arrlent, String user_id, String circle_id, String circle_name, String po_url) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("cat_name",circle_name) //todo 圈子名字
                .addParams("content", arrlent)   //todo 内容
                .addParams("cat_id", circle_id)  //todo  圈子id
                .addParams("po_url", po_url)  //todo 图片路径
                .addParams("cus_id", user_id)  //todo 发帖人id
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========上传============++++"+strresponse);
                        final PostedEditing00Bean postedEditing00Bean =   gson.fromJson(strresponse,PostedEditing00Bean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(postedEditing00Bean.errorCode ==200){

                                    EventBus.getDefault().post(new EventRefreshAttentionCicle("TrueCicle") );
                                    popupWindow.dismiss();
                                    flag_01 = true;
                                    Toast.makeText(ReleaseEditingActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                                    if( flag_02){
                                        finish();
                                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                    }else{
                                        flag_02 = false;
                                    }

                                }else{
                                    popupWindow.dismiss();
                                    flag_01 = true;
                                    Toast.makeText(ReleaseEditingActivity.this, "发布失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        flag_01 = true;
                        popupWindow.dismiss();
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }
    /**
     * @desc 图片提交后台转换成路径  1张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_01(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                        //Log.e("tag","+++++loadDate+++=========11111============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }
    /**
     * @desc 图片提交后台转换成路径  2张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_02(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========22222============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 图片提交后台转换成路径  3张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_03(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========3333============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 图片提交后台转换成路径  4张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_04(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========4444============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 图片提交后台转换成路径  5张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_05(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .addFile("cus_logo_05", mylist.get(4).getName(), mylist.get(4))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                        //Log.e("tag","+++++loadDate+++=========5555============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 图片提交后台转换成路径  6张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_06(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .addFile("cus_logo_06", mylist.get(5).getName(), mylist.get(5))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========6666============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 图片提交后台转换成路径  7张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_07(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .addFile("cus_logo_06", mylist.get(5).getName(), mylist.get(5))//
                .addFile("cus_logo_07", mylist.get(6).getName(), mylist.get(6))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========7777============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 图片提交后台转换成路径  8张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_08(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .addFile("cus_logo_06", mylist.get(5).getName(), mylist.get(5))//
                .addFile("cus_logo_07", mylist.get(6).getName(), mylist.get(6))//
                .addFile("cus_logo_08", mylist.get(7).getName(), mylist.get(7))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========88888============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 图片提交后台转换成路径  9张图片
     * @param mylist
     * @param arrlent   */
    private void changePath_09(List<File> mylist, final String arrlent) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", mylist.get(0).getName(), mylist.get(0))//
                .addFile("cus_logo_02", mylist.get(1).getName(), mylist.get(1))//
                .addFile("cus_logo_03", mylist.get(2).getName(), mylist.get(2))//
                .addFile("cus_logo_04", mylist.get(3).getName(), mylist.get(3))//
                .addFile("cus_logo_06", mylist.get(5).getName(), mylist.get(5))//
                .addFile("cus_logo_07", mylist.get(6).getName(), mylist.get(6))//
                .addFile("cus_logo_08", mylist.get(7).getName(), mylist.get(7))//
                .addFile("cus_logo_09", mylist.get(8).getName(), mylist.get(8))//
                .url(RBConstants.SERVER_URL+RBConstants.UOLOAD_PICTURE)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                       // Log.e("tag","+++++loadDate+++=========9999============++++"+strresponse);
                        final LoadPicturesDateBeans loadPicturesDateBeans =    gson.fromJson(strresponse, LoadPicturesDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(loadPicturesDateBeans.errorCode == 200){
                                    List<String>  bean = loadPicturesDateBeans.data;
                                    if(bean != null && bean.size() >0){
                                        // todo  图片转换成json 数组
                                        String filestr = tojson(bean);
                                        submitPictures_01(arrlent,user_id,circle_id,circle_name,filestr);
                                    }
                                }else{
                                    Toast.makeText(ReleaseEditingActivity.this, loadPicturesDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     *
     * @desc 上传视频
     * */
    private void publishVideo(String arrlent, File file) {
        OkHttpUtils.post()//
                .addFile("video", file.getName(), file)//
                .url(RBConstants.SERVER_URL+RBConstants.UPLOAD_VIDEO)
                .addParams("cat_name",circle_name)
                .addParams("content", arrlent)
                .addParams("cat_id", circle_id)
                .addParams("cus_id", user_id)  //todo 发帖人id
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String strresponse = response.body().string();
                        Gson gson = new Gson();
                         //Log.e("tag","+++++loadDate+++=========######@@@============++++"+strresponse);
                        final VideoPostDateBeans videoPostDateBeans = gson.fromJson(strresponse, VideoPostDateBeans.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(videoPostDateBeans.errorCode == 200){
                                    EventBus.getDefault().post(new EventRefreshAttentionCicle("TrueCicle") );
                                    popupWindow.dismiss();
                                    flag = true;
                                    Toast.makeText(ReleaseEditingActivity.this, "视频上传成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                }else{
                                    popupWindow.dismiss();
                                    flag = true;
                                    Toast.makeText(ReleaseEditingActivity.this, videoPostDateBeans.errorMessage, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        flag = true;
                        popupWindow.dismiss();
                        Toast.makeText(ReleaseEditingActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 图片转换成json 数组
     * */
    private String tojson(List<String> bean) {
        Gson gson = new Gson();
        String str = gson.toJson(bean);
        //TODO 这里打印一下str 看看是否得到的是json数组
       Log.e("tag","+++++loadDate+++=========&&&&&&&&&&&&============++++"+str);
        return  str;
    }



    /**
     * @desc 选择本地图片
     */
    public void selectpic() {
        Intent localIntent = new Intent("android.intent.action.PICK");
        localIntent.setType("image/*");
        startActivityForResult(localIntent, 1);
    }
    /**
     * @DES 选择本地视频
     */
    public void selectvideo() {
        Intent localIntent = new Intent("android.intent.action.PICK");
        localIntent.setType("video/*");
        startActivityForResult(localIntent, 2);
    }
    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
